using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace System.Windows.Forms.ColorControls
{
    /// <summary>
    /// A popup window that mimics the Visual Studio .NET color selector
    /// </summary>
    public class ColorPickerPopup : System.Windows.Forms.PopupForm
    {
        public static readonly Color[] VSColorPalette = new Color[] {
            System.Drawing.Color.White,
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(255))),
            System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(128))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(128))),
            System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(128))),
            System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(255)), ((System.Byte)(255))),
            System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255))),
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(255))),
            System.Drawing.Color.Silver,
            System.Drawing.Color.Red,
            System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0))),
            System.Drawing.Color.Yellow,
            System.Drawing.Color.Lime,
            System.Drawing.Color.Aqua,
            System.Drawing.Color.Blue,
            System.Drawing.Color.Magenta,
            System.Drawing.Color.Gray,
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(64)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(192))),
            System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64))),
            System.Drawing.Color.Maroon,
            System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0))),
            System.Drawing.Color.Olive,
            System.Drawing.Color.Green,
            System.Drawing.Color.Teal,
            System.Drawing.Color.Navy,
            System.Drawing.Color.Purple,
            System.Drawing.Color.Black,
            System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(0)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(64))),
            System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(64)), ((System.Byte)(0))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(64)), ((System.Byte)(64))),
            System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(64))),
            System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(0)), ((System.Byte)(64)))
        };

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Panel buttonPanel;
        private System.Windows.Forms.ColorDialog colorDialog1;
        private System.ComponentModel.IContainer components = null;
        private Color currentColor = Color.White;
		private System.Windows.Forms.ColorControls.ColorPanel customColorPanel;
        private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ColorControls.ColorList systemColorList;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.ColorControls.ColorList webColorList;

        /// <summary>
        /// Default constructor
        /// </summary>
        public ColorPickerPopup()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();

            // Load custom color palette
            this.customColorPanel.CustomColors = VSColorPalette;
        }

        /// <summary>
        /// Fires when color changed
        /// </summary>
        public event ColorChangedEventHandler ColorChanged;

        /// <summary>
        /// Set the current color to display in the popup
        /// </summary>
        [DefaultValue(typeof(Color), "White")]
        public Color Color
        {
            get
            {
                return currentColor;
            }
            set
            {
                currentColor = value;
                if (value.IsSystemColor)
                {
                    tabControl1.SelectedIndex = 2;
                }
                else if (value.IsKnownColor)
                {
                    tabControl1.SelectedIndex = 1;
                }
                else
                {
                    tabControl1.SelectedIndex = 0;
                }
                OnColorChanged(new ColorChangedEventArgs(value));
            }
        }

        /// <summary>
        /// Get/set the display of a button that launches the custom color dialog
        /// </summary>
        [DefaultValue(true),
        Category("Appearance")]
        [Description("Get/set the display of a button that launches the custom color dialog")]
        public bool ShowCustomButton
        {
            get
            {
                return buttonPanel.Visible;
            }
            set
            {
                buttonPanel.Visible = value;
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        /// <summary>
        /// Method called when color changed
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnColorChanged(ColorChangedEventArgs e)
        {
            if (ColorChanged != null)
                ColorChanged(this, e);
            if (Visible)
                Pop();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Pop();
            if (colorDialog1.ShowDialog(this.OwnerControl) == DialogResult.OK)
            {
                Color = colorDialog1.Color;
            }
        }

        /*
        private bool showCustom = true, showWeb = true, showSystem = true;

        [DefaultValue(true)]
        public bool ShowCustomColors
        {
            get { return showCustom; }
            set
            {
                if (showCustom != value)
                {
                    showCustom = value;
                    if (value)
                        tabPage1.Show();
                    else
                        tabPage1.Hide();
                    Refresh();
                }
            }
        }

        [DefaultValue(true)]
        public bool ShowWebColors
        {
            get { return showWeb; }
            set
            {
                if (showWeb != value)
                {
                    showWeb = value;
                    if (value)
                        tabPage2.Show();
                    else
                        tabPage2.Hide();
                    Refresh();
                }
            }
        }

        [DefaultValue(true)]
        public bool ShowSystemColors
        {
            get { return showSystem; }
            set
            {
                if (showSystem != value)
                {
                    showSystem = value;
                    if (value)
                        tabPage3.Show();
                    else
                        tabPage3.Hide();
                    Refresh();
                }
            }
        }
        */
		private void customColorPanel_ColorChanged(object sender, System.Windows.Forms.ColorControls.ColorChangedEventArgs e)
        {
            Color = e.Color;
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
			this.customColorPanel = new System.Windows.Forms.ColorControls.ColorPanel();
            this.tabPage2 = new System.Windows.Forms.TabPage();
			this.webColorList = new System.Windows.Forms.ColorControls.ColorList();
            this.tabPage3 = new System.Windows.Forms.TabPage();
			this.systemColorList = new System.Windows.Forms.ColorControls.ColorList();
            this.buttonPanel = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.colorDialog1 = new System.Windows.Forms.ColorDialog();
            this.panel1.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.tabPage3.SuspendLayout();
            this.buttonPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.customColorPanel)).BeginInit();
            this.SuspendLayout();
            //
            // panel1
            //
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                 this.tabControl1});
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(220, 224);
            this.panel1.TabIndex = 0;
            //
            // tabControl1
            //
            this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                      this.tabPage1,
                                                                                      this.tabPage2,
                                                                                      this.tabPage3});
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(218, 222);
            this.tabControl1.TabIndex = 1;
            this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
            //
            // tabPage1
            //
            this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.buttonPanel,
                                                                                   this.customColorPanel});
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(210, 196);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Custom";
            //
            // customColorPanel
            //
            this.customColorPanel.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.customColorPanel.ColorSet = System.Windows.Forms.ColorControls.ColorSet.Custom;
			this.customColorPanel.ColorSortOrder = System.Windows.Forms.ColorControls.ColorSortOrder.Unsorted;
            this.customColorPanel.ColorWellPadding = new System.Drawing.Size(2, 2);
            this.customColorPanel.ColorWellSize = new System.Drawing.Size(26, 26);
            this.customColorPanel.Columns = 8;
            this.customColorPanel.Dock = System.Windows.Forms.DockStyle.Top;
            this.customColorPanel.Name = "customColorPanel";
            this.customColorPanel.ShowFocus = false;
            this.customColorPanel.Size = new System.Drawing.Size(210, 106);
			this.customColorPanel.Style = System.Windows.Forms.ColorControls.ColorPanelStyle.VisualStudio;
            this.customColorPanel.TabIndex = 0;
            this.customColorPanel.TabStop = false;
			this.customColorPanel.ColorChanged += new System.Windows.Forms.ColorControls.ColorChangedEventHandler(this.customColorPanel_ColorChanged);
            //
            // tabPage2
            //
            this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.webColorList});
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(210, 164);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Web";
            this.tabPage2.Visible = false;
            //
            // webColorList
            //
            this.webColorList.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.webColorList.ColorSet = System.Windows.Forms.ColorControls.ColorSet.Web;
            this.webColorList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webColorList.IntegralHeight = false;
            this.webColorList.Name = "webColorList";
            this.webColorList.Size = new System.Drawing.Size(210, 164);
            this.webColorList.TabIndex = 0;
            this.webColorList.TabStop = false;
            this.webColorList.SelectedIndexChanged += new System.EventHandler(this.webColorList_SelectedIndexChanged);
            //
            // tabPage3
            //
            this.tabPage3.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.systemColorList});
            this.tabPage3.Location = new System.Drawing.Point(4, 22);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Size = new System.Drawing.Size(210, 164);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "System";
            this.tabPage3.Visible = false;
            //
            // systemColorList
            //
            this.systemColorList.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.systemColorList.ColorSet = System.Windows.Forms.ColorControls.ColorSet.System;
            this.systemColorList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.systemColorList.ForeColor = System.Drawing.Color.White;
            this.systemColorList.IntegralHeight = false;
            this.systemColorList.Name = "systemColorList";
            this.systemColorList.Size = new System.Drawing.Size(210, 164);
            this.systemColorList.TabIndex = 0;
            this.systemColorList.TabStop = false;
            this.systemColorList.SelectedIndexChanged += new System.EventHandler(this.sytemColorList_SelectedIndexChanged);
            //
            // buttonPanel
            //
            this.buttonPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                      this.button1});
            this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Top;
            this.buttonPanel.DockPadding.Bottom = 2;
            this.buttonPanel.DockPadding.Left = 2;
            this.buttonPanel.DockPadding.Right = 2;
            this.buttonPanel.DockPadding.Top = 7;
            this.buttonPanel.Location = new System.Drawing.Point(0, 106);
            this.buttonPanel.Name = "buttonPanel";
            this.buttonPanel.Size = new System.Drawing.Size(210, 35);
            this.buttonPanel.TabIndex = 1;
            //
            // button1
            //
            this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.button1.Location = new System.Drawing.Point(2, 7);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(206, 26);
            this.button1.TabIndex = 2;
            this.button1.Text = "More Colors...";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // ColorPickerPopup
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(220, 224);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.panel1});
            this.Name = "ColorPickerPopup";
            this.panel1.ResumeLayout(false);
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            this.tabPage3.ResumeLayout(false);
            this.buttonPanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.customColorPanel)).EndInit();
            this.ResumeLayout(false);
        }

        private void sytemColorList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (systemColorList.SelectedItem is Color)
                Color = (Color)systemColorList.SelectedItem;
            else if (systemColorList.SelectedItem is string)
                Color = Color.FromName((string)systemColorList.SelectedItem);
        }

        private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    customColorPanel.Color = currentColor;
                    break;
                case 1:
                    webColorList.SelectedIndex = webColorList.Items.IndexOf(currentColor);
                    break;
                case 2:
                default:
                    systemColorList.SelectedIndex = systemColorList.Items.IndexOf(currentColor);
                    break;
            }
        }

        private void webColorList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (webColorList.SelectedItem is Color)
                Color = (Color)webColorList.SelectedItem;
            else if (webColorList.SelectedItem is string)
                Color = Color.FromName((string)webColorList.SelectedItem);
        }
    }
}